<?php
class Slash_ACL_Property_AclRestrictions extends Sabre_DAV_Property
{
	/*
   * Copyright © 2010 Jack Cleaver
   * 
   * This plugin provides functionality added by CalDAV (RFC 4791)
   * It implements new reports, and the MKCALENDAR method.
   *
   * This file is part of Slash.
   *
   * Slash is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Slash is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with Slash.  If not, see <http://www.gnu.org/licenses/>.
	 */
	
	private $grantOnly = false;
	private $noInvert = false;
	private $denyBefore = false;
	private $requiredPrics = array();
	
	public function __construct($grantOnly, $noInvert, $denyBefore, $requiredPrincs)
	{
		$this->grantOnly = $grantOnly;
		$this->noInvert = $noInvert;
		$this->denyBefore = $denyBefore;
		$this->requiredPrincs = $requiredPrincs; 
	}

	static function unserialize(DOMElement $aclrElement) 
	{
		$grantOnly = ($aclrElement->getElementsByTagNameNS("urn:DAV", "grant-only")->length == 1);
		$noInvert = ($aclrElement->getElementsByTagNameNS("urn:DAV", "no-invert")->length == 1);
		$denyBefore = ($aclrElement->getElementsByTagNameNS("urn:DAV", "deny-before-grant")->length == 1);
		$denyBefore = ($aclrElement->getElementsByTagNameNS("urn:DAV", "deny-before-grant")->length == 1);
		$requiredPrincs = array();
		$nlrp = $aclrElement->getElementsByTagNameNS("urn:DAV", "required-principal");
		$len = $nlrp->length;
		for ($i=0; $i < $len; $i++)
		{
			$requiredPrincs[] = Slash_ACL_Property_Principal::unserialize($nlrp->item($i));
		}
		return new self($grantOnly, $noInvert, $denyBefore, $requiredPrincs);
	}

	public function serialize(Sabre_DAV_Server $server, DOMElement $aclr) 
  {
  	$prefix = $server->xmlNamespaces['DAV:'];
    $doc = $aclr->ownerDocument;
   	if ($this->grantOnly)
   	{
   		$aclr->appendChild($doc->createElement("$prefix:grant-only"));
   	}
   	if ($this->noInvert)
   	{
   		$aclr->appendChild($doc->createElement("$prefix:no-invert"));
   	}
   	if ($this->denyBefore)
   	{
   		$aclr-appendChild($doc->createElement("$prefix:deny-before"));
   	}
   	foreach ($this->requiredPrincs as $princ)
   	{
   		$rpElement = $doc->createElement("$prefix:required-principal");
   		$princ->serialize($server, $rpElement);
   		$aclr->appendChild($rpElement);
   	}
   	$aclr->normalize(); 
  }
}